package com.example.leave.controller;

import com.example.leave.common.CommonPage;
import com.example.leave.common.CommonResult;
import com.example.leave.component.equator.FieldInfo;
import com.example.leave.dto.LeaveFormDto;
import com.example.leave.form.*;
import com.example.leave.service.LeaveService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author huang
 */
@Api(tags = "请假管理")
@Controller
@RequestMapping("/leave")
public class LeaveController {

    @Autowired
    LeaveService leaveService;

    @ApiOperation("添加请假表单")
    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<Boolean> add(@RequestBody @Valid LeaveFormForm leaveFormForm) {
        boolean isSuccess = leaveService.add(leaveFormForm);
        if (isSuccess) {
            return CommonResult.success(true, "请假申请已提交");
        } else {
            return CommonResult.error(false, "该时间段已有请假信息，无法再次申请");
        }
    }

    @ApiOperation("通过id删除单个请假表单")
    @RequestMapping(value = "delOneById", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<Boolean> delOneById(@NotBlank @RequestBody String id) {
        if (leaveService.delOneById(id)) {
            return CommonResult.success(true, "请假申请已提交");
        } else {
            return CommonResult.error(false, "该时间段已有请假信息，无法再次申请");
        }
    }

    @ApiOperation("通过id列表批量删除请假表单")
    @RequestMapping(value = "delByIdList", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<Integer> delByIdList(@RequestBody @NotNull List<String> ids) {
        int delCount = leaveService.delByIdList(ids);
        return CommonResult.success(delCount, "已删除" + delCount + "条数据");
    }

    @ApiOperation("通过id更新请假表单")
    @RequestMapping(value = "updateById", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<List<FieldInfo>> updateById(@RequestBody @Valid IdAndLeaveFormForm idAndLeaveFormForm) {
        return CommonResult.success(leaveService.updateById(idAndLeaveFormForm), "更新成功");
    }

    @ApiOperation("查询某个时间段有请假的请假信息")
    @RequestMapping(value = "findWhoWasAbsentAtSometime", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<CommonPage<LeaveFormDto>> findWhoWasAbsentAtSometime(
            @RequestBody @Valid TimeConditionForm timeConditionForm) {

        return CommonResult.success(leaveService.findWhoWasAbsentAtSometime(timeConditionForm), "查询成功");
    }

    @ApiOperation("查询单个用户在某个时间段请假的请假单")
    @RequestMapping(value = "findByUserIdAndStartTimeBetween", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<CommonPage<LeaveFormDto>> findByUserIdAndStartTimeBetween(
            @RequestBody @Valid IdAndTimeBetweenAndPageInfoVoForm idAndTimeBetweenAndPageInfoVo) {
        return CommonResult.success(leaveService
                .findByUserIdAndStartTimeBetween(idAndTimeBetweenAndPageInfoVo), "查询成功");
    }

    @ApiOperation("通过原因进行LIKE搜索")
    @RequestMapping(value = "findByReasonLike", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<CommonPage<LeaveFormDto>> findByReasonLike(
            @Valid @RequestBody ConditionAndPageInfoForm reasonAndPageInfoVo) {

        return CommonResult.success(leaveService.findByReasonLike(reasonAndPageInfoVo), "查询成功");
    }

    @ApiOperation("通过分类进行LIKE搜索")
    @RequestMapping(value = "findByCategoryLike", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<CommonPage<LeaveFormDto>> findByCategoryLike(
            @Valid @RequestBody ConditionAndPageInfoForm categoryAndPageInfoVo) {
        return CommonResult.success(leaveService.findByCategoryLike(categoryAndPageInfoVo), "查询成功");
    }

    @ApiOperation("通过Id搜索")
    @RequestMapping(value = "findById", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<LeaveFormDto> findById(@NotBlank @RequestBody String id) {
        LeaveFormDto leaveFormDto = leaveService.findById(id);
        if (null != leaveFormDto) {
            return CommonResult.success(leaveFormDto, "查询成功");
        }
        return CommonResult.error("查询不到id为" + id + "的请假表单");
    }

    @ApiOperation("通过开始时间区间搜索")
    @RequestMapping(value = "findByStartTimeBetween", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<CommonPage<LeaveFormDto>> findByStartTimeBetween(
            @RequestBody @Valid TimeBetweenAndPageInfoForm startTimeBetweenAndPageInfoForm) {
        return CommonResult.success(leaveService.findByStartTimeBetween(startTimeBetweenAndPageInfoForm), "查询成功");
    }

    @ApiOperation("分页查询所有")
    @RequestMapping(value = "getAll", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<CommonPage<LeaveFormDto>> getAll(@RequestBody @Valid PageInfoForm pageInfoForm) {
        return CommonResult.success(leaveService.getAll(pageInfoForm));
    }


    @ApiOperation("查询单个用户在某个时间段请假的请假单")
    @RequestMapping(value = "isSomeoneHasLeaveAtSometime", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<Boolean> isSomeoneHasLeaveAtSometime(@RequestBody @Valid IdAndTimeBetweenForm userIdAndTimeBetweenForm) {
        return CommonResult.success(leaveService
                .isSomeoneHasLeaveAtSometime(userIdAndTimeBetweenForm), "查询成功");
    }


}
